## do congress-by-congress regressions

dta <- read.table("rollcall.logit.inter.all.txt",header=T)

n.period <- max(dta$period)

b.ds <- array(NA,c(5,8))
se.ds <- array(NA,c(5,8))

for (j in 1:n.period) {

  dta.ds <- subset(dta,region==1 & period==j)
    
  out.ds <- glm(prolabor ~ unionpop + urbanpct +
                aapct + laborcomm, family=binomial(link="probit"), data=dta.ds)

  cat("region ",unique(dta.ds$region),"period ",j,"\n")
  print(summary(out.ds))
  
  b.ds[,j] <- coef(out.ds)
  se.ds[,j] <- sqrt(diag(vcov(out.ds)))  
  
}

b.bs <- array(NA,c(5,8))
se.bs <- array(NA,c(5,8))

for (j in 1:n.period) {

  dta.bs <- subset(dta,region==2 & period==j)
    
  out.bs <- glm(prolabor ~ unionpop + urbanpct +
                aapct + laborcomm, family=binomial(link="probit"), data=dta.bs)

  cat("region ",unique(dta.bs$region),"period ",j,"\n")
  print(summary(out.bs))
  
  b.bs[,j] <- coef(out.bs)
  b.na.bs <- as.matrix(na.omit(coef(out.bs)))
  if (length(coef(out.bs)) != length(b.na.bs)) {
    se.bs[,j] <- c(sqrt(diag(vcov(out.bs))),NA)
  } else {
    se.bs[,j] <- sqrt(diag(vcov(out.bs)))  
  }
}

b.ns <- array(NA,c(6,8))
se.ns <- array(NA,c(6,8))

for (j in 1:n.period) {

  dta.ns <- subset(dta,region==3 & period==j)
    
  out.ns <- glm(prolabor ~ unionpop + urbanpct + aapct +
                laborcomm + dem, family=binomial(link="probit"),
                data=dta.ns)


  cat("region ",unique(dta.ns$region),"period ",j,"\n")
  print(summary(out.ns))
  
  b.ns[,j] <- coef(out.ns)
  se.ns[,j] <- sqrt(diag(vcov(out.ns)))  
  
}

congnum <- seq(73,80,1)

regression.plots <- function (ests,ses,varname,regionname) {

  lb <- ests - 1.64*ses
  ub <- ests + 1.64*ses
  
  b.range <- range(lb,ub)+range(lb,ub)*.01
  
  plot(congnum,ests,ylim=b.range,pch=20,cex.axis=.8,xlab="Congress",ylab="Estimates",main=list(varname,cex=.9),sub=regionname)
  
  segments(congnum,lb,congnum,ub)
  
  lines (c(72,81), c(0,0),lty=3)

}

pdf (paste(file="votes_bycongress_regression_figs.pdf"))

par(mfrow=c(4,3))

regression.plots(b.ds[1,],se.ds[1,],"Region-Period Constant","Deep South")
regression.plots(b.bs[1,],se.bs[1,],"Region-Period Constant","Border South")
regression.plots(b.ns[1,],se.ns[1,],"Region-Period Constant","Non-South")

regression.plots(b.ds[2,],se.ds[2,],"Union Pct","Deep South")
regression.plots(b.bs[2,],se.bs[2,],"Union Pct","Border South")
regression.plots(b.ns[2,],se.ns[2,],"Union Pct","Non-South")

regression.plots(b.ds[4,],se.ds[4,],"African-American Pct","Deep South")
regression.plots(b.bs[4,],se.bs[4,],"African-American Pct","Border South")
regression.plots(b.ns[4,],se.ns[4,],"African-American Pct","Non-South")

regression.plots(b.ds[3,],se.ds[3,],"Urban Pct","Deep South")
regression.plots(b.bs[3,],se.bs[3,],"Urban Pct","Border South")
regression.plots(b.ns[3,],se.ns[3,],"Urban Pct","Non-South")

graphics.off()

a.names <- c("73rd,Deep South","73rd,Border South","73rd,Non-South","74th,Deep South","74th,Border South","74th,Non-South","75th,Deep South","75th,Border South","75th,Non-South","76th,Deep South","76th,Border South","76th,Non-South","77th,Deep South","77th,Border South","77th,Non-South","78th,Deep South","78th,Border South","78th,Non-South","79th,Deep South","79th,Border South","79th,Non-South","80th,Deep South","80th,Border South","80th,Non-South")

a.names.alt <- t(matrix(a.names,3,8))

par(mfrow=c(1,2))

rnames <- a.names.alt

texfile <- "rc_probits.tex"

roundval <- 3

  cat("\\begin{table}\\centering \\small \\ssp \n ",file=texfile,append=FALSE)
  cat("\\begin{threeparttable}\n ",file=texfile,append=TRUE)
  cat("\\caption{Results from congess-by-congress regressions analysis of labor roll call votes in the Senate (73rd--80th Congresses)} \n \\label{tab:rc_probits} \n",file=texfile,append=TRUE)

  cat("\\begin{tabular}{ld{-1}d{-1}d{-1}d{-1}d{-1}d{-1}} \n \\toprule \n \\midrule \n",file=texfile,append=TRUE)
  cat("& \\multicolumn{1}{c}{Intercept} & \\multicolumn{1}{c}{Union} & \\multicolumn{1}{c}{AA\\%} & \\multicolumn{1}{c}{Urban \\%} & \\multicolumn{1}{c}{Labor Comm.} &  \\multicolumn{1}{c}{Democrat} \\\\ \n",file=texfile,append=TRUE)

  names.ds <- rnames[,1]
  
  for (t in 1:length(names.ds)){
    cat(names.ds[t],file=texfile,append=TRUE)
    for (k in 1:nrow(b.ds)){
      est <- b.ds[k,t]
      se <-  se.ds[k,t]
      if ( !is.na(est) ) {
        pval <- pnorm(abs(est/se), lower.tail=F)*1.64
        siglevel <- ifelse(pval <= 0.1,"^{*}","")

        cat (paste(" & ", round(est,roundval),siglevel),file=texfile,append=TRUE)
      }  else {
        cat (" & \\cdot ",file=texfile,append=TRUE)
      }
    }
    cat (" & \\cdot \\\\\n ",file=texfile,append=TRUE)
  }
  
cat("\n \\midrule \n",file=texfile,append=TRUE)

  names.bs <- rnames[,2]
  
  for (t in 1:length(names.bs)){
    cat(names.bs[t],file=texfile,append=TRUE)
    for (k in 1:nrow(b.bs)){
      est <- b.bs[k,t]
      se <-  se.bs[k,t]
      if ( !is.na(est) ) {
        pval <- pnorm(abs(est/se), lower.tail=F)*1.64
        siglevel <- ifelse(pval <= 0.1,"^{*}","")

        cat (paste(" & ", round(est,roundval),siglevel),file=texfile,append=TRUE)
      }   else {
        cat (" & \\cdot ",file=texfile,append=TRUE)
      }
    }
    cat (" & \\cdot \\\\\n ",file=texfile,append=TRUE)
  }
  
cat("\n \\midrule \n",file=texfile,append=TRUE)


  names.ns <- rnames[,3]
  
  for (t in 1:length(names.ns)){
    cat(names.ns[t],file=texfile,append=TRUE)
    for (k in 1:(nrow(b.ns))){
      est <- b.ns[k,t]
      se <-  se.ns[k,t]
      if ( !is.na(est) ) {
        pval <- pnorm(abs(est/se), lower.tail=F)*1.64
        siglevel <- ifelse(pval <= 0.1,"^{*}","")

        cat (paste(" & ", round(est,roundval),siglevel),file=texfile,append=TRUE)
      }
    }
    cat (" \\\\\n ",file=texfile,append=TRUE)
  }
  
  cat("\\\\ \\bottomrule \n \\end{tabular}\n \\begin{tablenotes} \n \\item \n {\\em Notes}: \\ssp $^* p \\leq .1$. Table entries are probit maximum likelihood coefficient estimates. \\textit{Union} indicates union density, \\textit{AA} indicates percent African-American, \\textit{Urban} indicates percent urbanization, and \\textit{Labor Committee} and \\textit{Democrat} refer to dummy variables indicating membership on the labor committee and in the Democratic party, respectively. \n \\end{tablenotes} \n \\end{threeparttable} \n \\hspace{\\fill} \n \\end{table}\n \n \n",file=texfile,append=TRUE)

  
  

